﻿using JsxyUIS.WebClient.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JsxyUIS.WebClient.Controllers
{
    public class HomeController : Controller
    {
        private List<TreeNode> navMenu;

        public HomeController()
        {
            this.navMenu = new List<TreeNode>();
            this.navMenu.Add(new TreeNode() { id = 1, text = "个人基本信息", iconCls = "icon-hamburg-my-account" });
            this.navMenu.Add(new TreeNode() { id = 2, text = "子应用系统快速访问", iconCls = "icon-standard-application-cascade" });
            this.navMenu.Add(new TreeNode() { id = 3, text = "新闻管理", iconCls = "icon-hamburg-graphy" });
            this.navMenu.Add(new TreeNode() { id = 4, text = "系统设置", iconCls = "icon-hamburg-config" });

            this.navMenu.Add(new TreeNode() { id = 11, text = "个人信息查看", pid = 1, attributes = new { href = VirtualPathUtility.ToAbsolute("~/User/InfoView") } });
            this.navMenu.Add(new TreeNode() { id = 12, text = "修改密码", pid = 1, attributes = new { href = VirtualPathUtility.ToAbsolute("~/User/UpdatePassword") } });

            this.navMenu.Add(new TreeNode() { id = 21, text = "办公自动化系统", pid = 2, attributes = new { href = VirtualPathUtility.ToAbsolute("~/Application/RenderTo?appid=1") } });
            this.navMenu.Add(new TreeNode() { id = 22, text = "教务在线系统", pid = 2, attributes = new { href = VirtualPathUtility.ToAbsolute("~/Application/RenderTo?appid=2") } });
            this.navMenu.Add(new TreeNode() { id = 23, text = "图书馆在线系统", pid = 2, attributes = new { href = VirtualPathUtility.ToAbsolute("~/Application/RenderTo?appid=3") } });

            this.navMenu.Add(new TreeNode() { id = 31, text = "新闻类型管理", pid = 3, attributes = new { href = VirtualPathUtility.ToAbsolute("~/NewsManage/NewsType") } });
            this.navMenu.Add(new TreeNode() { id = 32, text = "新闻发布管理", pid = 3, attributes = new { href = VirtualPathUtility.ToAbsolute("~/NewsManage/NewsInfo") } });

            this.navMenu.Add(new TreeNode() { id = 41, text = "子应用系统管理", pid = 4, attributes = new { href = VirtualPathUtility.ToAbsolute("~/SysSetting/Application") } });
            this.navMenu.Add(new TreeNode() { id = 42, text = "教职人员信息管理", pid = 4, attributes = new { href = VirtualPathUtility.ToAbsolute("~/SysSetting/Employee") } });
            this.navMenu.Add(new TreeNode() { id = 43, text = "学生信息管理", pid = 4, attributes = new { href = VirtualPathUtility.ToAbsolute("~/SysSetting/Student") } });
            this.navMenu.Add(new TreeNode() { id = 44, text = "部门信息查看", pid = 4, attributes = new { href = VirtualPathUtility.ToAbsolute("~/SysSetting/Department") } });
        }

        public ActionResult Index()
        {
            var model = new MainViewModel { UserCode = "000", LoginCode = "admin", UserName = "系统管理员", UserDepartment = "信息技术部" };
            return View(model);
        }

        public JsonResult GetNavMenu() 
        {
            var menu = from m in this.NavMenu where m.pid == 0 select m;
            return this.Json(menu);
        }

        public JsonResult GetSubMenu(int pid)
        {
            var menu = from m in this.NavMenu where m.pid == pid select m;
            return this.Json(menu);
        }


        [AllowAnonymous]
        public ViewResult Login()
        {
            var model = new LoginViewModel();
            var loginCode = this.Request.Cookies["loginCode"];
            if (loginCode != null) 
            {
                model.LoginCode = loginCode.Value;
                var password = this.Request.Cookies["password"];
                if (password != null)
                {
                    model.Password = password.Value;
                }
                var rememberMe = this.Request.Cookies["rememeberMe"];
                if (rememberMe != null)
                {
                    bool remember = false;
                    bool.TryParse(rememberMe.Value, out remember);
                    model.RememberMe = remember;
                }
            }
            return View(model);
        }

        [HttpPost]
        [AllowAnonymous]
        //[ValidateAntiForgeryToken]
        public ActionResult Login(LoginViewModel model)
        {
            if (model.LoginCode == "000" && model.Password == "123")
            {
                this.Response.Cookies["loginCode"].Value = model.LoginCode;
                this.Response.Cookies["rememberMe"].Value = model.RememberMe.ToString();
                return this.Json(new { status = true, message = "登录成功!", url = VirtualPathUtility.ToAbsolute("~/Home/Index") });
            }
            else 
            {
                return this.Json(new { status = false, message = "登录失败!", url = "" });
            }
        }


        public List<TreeNode> NavMenu 
        {
            get { return this.navMenu; }
        }


        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}